#-*- coding: utf-8 -*- 
  
import platform 
import sys 
import os 
import time 
import _thread 


all_ip_info = []
  
def get_os(): 
    ''''' 
    get os 类型 
    '''
    os = platform.system() 
    if os == "Windows": 
        return "n"
    else: 
        return "c"
    
def ping_ip(ip_str): 
    cmd = ["ping", "-{op}".format(op=get_os()), 
        "1", ip_str] 
    output = os.popen(" ".join(cmd)).readlines() 

    flag = False
    for line in list(output): 
        if not line: 
            continue
        if str(line).upper().find("TTL") >=0: 
            flag = True
            break
    # if flag: 
    #     print("ip: %s is ok ***" %ip_str)
    # else: 
    #     print("ip: %s is fail ***" % ip_str)
    # all_ip_info.append(ip_str + " " + ("ok" if flag else "fail"))
    if (flag):
        all_ip_info.append(ip_str)

        

def find_ip(ip_list):
    for ip in ip_list: 
        _thread.start_new_thread(ping_ip, (ip,))
        # ping_ip(ip)
        time.sleep(0.3)
    
if __name__ == "__main__": 
    # output = os.popen("arp -a").readlines()
    # # print(output)
    # for one in output:
    #     if ("192.168." in one):
    #         print(one)  

    print("start time %s"%time.ctime())
    ip_list = []
    for i in range(1,256): 
        ip_list.append("192.168.%s.%s"%(0, i))
    find_ip(ip_list) 
    print("end time %s"%time.ctime())
    for one in all_ip_info:
        print(one)

    output = os.popen("arp -a").readlines()
    for one in output:
        print(one, end='')
    print("######")
    for one in output:
        if ("192.168." in one and "---" not in one):
            print(one, end='')  
    # ping_ip("192.168.100.1")
    # ping_ip("192.168.100.201")